package com.learn.shop.service.impl;

import com.learn.shop.pojo.entriy.CrawlingNovelData;
import com.learn.shop.mapper.CrawlingNovelDataMapper;
import com.learn.shop.pojo.vo.BatchCrawlingNovelVo;
import com.learn.shop.service.CrawlingNovelDataService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * (CrawlingNovelData)表服务实现类
 *
 * @author zhengdechang
 * @since 2021-12-31 15:14:26
 */
@Service
public class CrawlingNovelDataServiceImpl implements CrawlingNovelDataService {
    @Autowired
    private CrawlingNovelDataMapper crawlingNovelDataMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CrawlingNovelData queryById(Integer id) {
        return this.crawlingNovelDataMapper.queryById(id);
    }

    @Override
    public void deleteAll() {
        this.crawlingNovelDataMapper.deleteAll();
    }

    @Override
    public List<CrawlingNovelData> list(BatchCrawlingNovelVo vo) {
        return crawlingNovelDataMapper.list(vo);
    }

    /**
     * 分页查询
     *
     * @param crawlingNovelData 筛选条件
     * @param pageRequest       分页对象
     * @return 查询结果
     */
    @Override
    public Page<CrawlingNovelData> queryByPage(CrawlingNovelData crawlingNovelData, PageRequest pageRequest) {
        long total = this.crawlingNovelDataMapper.count(crawlingNovelData);
        return new PageImpl<>(this.crawlingNovelDataMapper.queryAllByLimit(crawlingNovelData, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param crawlingNovelData 实例对象
     * @return 实例对象
     */
    @Override
    public CrawlingNovelData insert(CrawlingNovelData crawlingNovelData) {
        this.crawlingNovelDataMapper.insert(crawlingNovelData);
        return crawlingNovelData;
    }

    /**
     * 修改数据
     *
     * @param crawlingNovelData 实例对象
     * @return 实例对象
     */
    @Override
    public CrawlingNovelData update(CrawlingNovelData crawlingNovelData) {
        this.crawlingNovelDataMapper.update(crawlingNovelData);
        return this.queryById(crawlingNovelData.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.crawlingNovelDataMapper.deleteById(id) > 0;
    }
}
